# **********************************************************************
#
# Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
#
# This copy of Ice is licensed to you under the terms described in the
# ICE_LICENSE file included in this distribution.
#
# **********************************************************************

#
# Select an installation base directory. The directory will be created
# if it does not exist.
#
prefix			 ?= ${AS_INSTALL_DIR}/Ice-$(VERSION)

#
# The "root directory" for runpath embedded in executables. Can be unset
# to avoid adding a runpath to Ice executables.
#
embedded_runpath_prefix  ?= ${AS_INSTALL_DIR}/Ice-$(VERSION_MAJOR).$(VERSION_MINOR)

#
# Define OPTIMIZE as yes if you want to build with
# optimization. Otherwise Ice is build with debug information.
#
#OPTIMIZE		= yes

#
# Define LP64 as yes or no if you want force a 32 or 64 bit. The
# default is platform-dependent
#
#LP64			?= yes


#
# Define USE_READLINE as yes if you want to build parts of Ice using
# readline on platforms where readline is available (see
# PLATFORM_HAS_READLINE in Make.rules.$(UNAME))
#
# readline is used by a number of Ice admin programs to provide
# command history, an optional feature.
#
# readline is licensed by the Free Software Foundation under the GNU
# Public License v2, and cannot be combined with GPL-incompatible
# software in any program. In particular the OpenSSL license is
# GPL-incompatible.
#
# If you combine Ice with more GPL-incompatible software, or have
# licensed Ice under a commercial (non-GPL) license, you should ponder
# the licensing implications of using readline.
# 
# 这个东西很有用，能够记忆你以前输入的命令提交admin手工操作的效率
# 当然建议使用脚本进行操作
USE_READLINE		?= yes


#
# If libbzip2 is not installed in a standard location where the
# compiler can find it, set BZIP2_HOME to the bzip2 installation
# directory.
#
BZIP2_HOME		?= ${Ice_ThirdParty}/bzip2


#
# If Berkeley DB is not installed in a standard location where the
# compiler can find it, set DB_HOME to the Berkeley DB installation
# directory.
#
DB_HOME		?= ${Ice_ThirdParty}/db
#
# If expat is not installed in a standard location where the compiler
# can find it, set EXPAT_HOME to the expat installation directory.
#
EXPAT_HOME		?= ${Ice_ThirdParty}/expat

#
# If OpenSSL is not installed in a standard location where the
# compiler can find it, set OPENSSL_HOME to the OpenSSL installation
# directory.
#
OPENSSL_HOME		?= ${Ice_ThirdParty}/openssl

#
# If Mcpp is not installed in a standard location where the  compiler 
# can find it, set MCPP_HOME to the Mcpp installation directory.
#
MCPP_HOME		?= ${Ice_ThirdParty}/mcpp

#
# If readline is not installed in a standard location where the
# compiler can find it, AND you want to use readline, set
# READLINE_HOME to the readline installation directory.
#
#READLINE_HOME		?= /opt/readline

#
# Generate position independent code unless GENPIC is set to no
#
GENPIC                     ?= yes

# ----------------------------------------------------------------------
# Don't change anything below this line!
# ----------------------------------------------------------------------

#
# Common definitions
#
ice_language     = cpp
ifneq ($(shell test "$(USE_BIN_DIST)" != "yes" -a -d $(top_srcdir)/../$(ice_language) && echo 0),0)
    slice_translator = slice2cpp
    ice_require_cpp  = 1
endif

ifeq ($(shell test -f $(top_srcdir)/config/Make.common.rules && echo 0),0)
    include $(top_srcdir)/config/Make.common.rules
else
    include $(top_srcdir)/../config/Make.common.rules
endif

bindir			= $(top_srcdir)/bin
libdir			= $(top_srcdir)/lib
headerdir		= $(top_srcdir)/include

#
# includedir is not handled the same as bindir and libdir
# because it is used in the .depend files
#
ifdef ice_src_dist
    includedir		= $(top_srcdir)/include
else
    includedir		= $(ice_dir)/include
endif

#
# Platform specific definitions
#
include	 $(top_srcdir)/config/Make.rules.$(UNAME)

install_includedir	:= $(prefix)/include
install_docdir		:= $(prefix)/doc
install_bindir	  	:= $(prefix)/$(binsubdir)
install_libdir	  	:= $(prefix)/$(libsubdir)
install_configdir 	:= $(prefix)/config

ifneq ($(embedded_runpath_prefix),)
    runpath_libdir	:= $(embedded_runpath_prefix)/$(libsubdir)
    create_runpath_symlink ?= yes
endif

OPENSSL_FLAGS           = $(if $(OPENSSL_HOME),-I$(OPENSSL_HOME)/include)
OPENSSL_LIBS            = $(if $(OPENSSL_HOME),-L$(OPENSSL_HOME)/$(libsubdir)) -lssl -lcrypto
OPENSSL_RPATH_LINK      = $(if $(OPENSSL_HOME),$(call rpathlink,$(OPENSSL_HOME)/$(libsubdir)))

BZIP2_FLAGS             = $(if $(BZIP2_HOME),-I$(BZIP2_HOME)/include)
BZIP2_LIBS              = $(if $(BZIP2_HOME),-L$(BZIP2_HOME)/$(libsubdir)) -lbz2
BZIP2_RPATH_LINK        = $(if $(BZIP2_HOME),$(call rpathlink,$(BZIP2_HOME)/$(libsubdir)))

ifneq ($(DB_HOME),)
   DB_FLAGS             = -I$(DB_HOME)/include
   DB_LIBS              = -L$(DB_HOME)/$(libsubdir) -ldb_cxx
   DB_RPATH_LINK        = $(call rpathlink,$(DB_HOME)/$(libsubdir))
else
   ifeq ($(shell if [ -d /usr/include/db46 -a -d /usr/$(libsubdir)/db46 ] ; then echo yes; fi), yes)
      DB_FLAGS          = -I/usr/include/db46
      DB_LIBS           = -L/usr/$(libsubdir)/db46 -ldb_cxx
   else
      DB_LIBS           = -ldb_cxx
   endif
endif

EXPAT_FLAGS             = $(if $(EXPAT_HOME),-I$(EXPAT_HOME)/include)
EXPAT_LIBS              = $(if $(EXPAT_HOME),-L$(EXPAT_HOME)/$(libsubdir)) -lexpat
EXPAT_RPATH_LINK        = $(if $(EXPAT_HOME),$(call rpathlink,$(EXPAT_HOME)/$(libsubdir)))

MCPP_LIBS              = $(if $(MCPP_HOME),-L$(MCPP_HOME)/$(libsubdir)) -lmcpp
MCPP_RPATH_LINK        = $(if $(MCPP_HOME),$(call rpathlink,$(MCPP_HOME)/$(libsubdir)))

ifeq ($(PLATFORM_HAS_READLINE),yes)
    ifeq ($(USE_READLINE),yes)
        READLINE_FLAGS  = -DHAVE_READLINE $(if $(READLINE_HOME),-I$(READLINE_HOME)/include)
        READLINE_LIBS   = $(if $(READLINE_HOME),-L$(READLINE_HOME)/$(libsubdir)) -lreadline -lncurses        
    endif
endif

ICECPPFLAGS		= -I$(slicedir)
SLICE2CPPFLAGS		= $(ICECPPFLAGS)

ifeq ($(ice_dir), /usr) 
    CPPFLAGS		= 
    LDFLAGS		= $(LDPLATFORMFLAGS) $(CXXFLAGS)
else
    CPPFLAGS	= -I$(includedir)
    ifdef ice_src_dist
        LDFLAGS		= $(LDPLATFORMFLAGS) $(CXXFLAGS) -L$(libdir) 
    else
        LDFLAGS		= $(LDPLATFORMFLAGS) $(CXXFLAGS) -L$(ice_dir)/$(libsubdir) 
    endif
endif

ifeq ($(FLEX_NOLINE),yes)
    FLEXFLAGS	       := -L
else
    FLEXFLAGS	       :=
endif

ifeq ($(BISON_NOLINE),yes)
    BISONFLAGS		:= -dvtl
else
    BISONFLAGS		:= -dvt
endif

ifeq ($(mkshlib),)
    $(error You need to define mkshlib in Make.rules.$(UNAME)) 
endif

ifdef ice_src_dist
    SLICEPARSERLIB	= $(libdir)/$(call mklibfilename,Slice,$(VERSION))
    SLICE2CPP		= $(bindir)/slice2cpp
    SLICE2XSD		= $(bindir)/slice2xsd
    SLICE2FREEZE	= $(bindir)/slice2freeze
    SLICE2DOCBOOK	= $(bindir)/slice2docbook
else
    SLICEPARSERLIB	= $(ice_dir)/$(libsubdir)/$(call mklibfilename,Slice,$(VERSION))
    SLICE2CPP		= $(ice_dir)/$(binsubdir)/slice2cpp
    SLICE2XSD		= $(ice_dir)/$(binsubdir)/slice2xsd
    SLICE2FREEZE	= $(ice_dir)/$(binsubdir)/slice2freeze
    SLICE2DOCBOOK	= $(ice_dir)/$(binsubdir)/slice2docbook
endif

EVERYTHING		= all depend clean install 

.SUFFIXES:
.SUFFIXES:		.cpp .c .o

.cpp.o:
	$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $<

.c.o:
	$(CC) -c $(CPPFLAGS) $(CFLAGS) $<


$(HDIR)/%F.h: $(SDIR)/%F.ice $(SLICE2CPP) $(SLICEPARSERLIB)
	rm -f $(HDIR)/$(*F)F.h $(*F)F.cpp
	$(SLICE2CPP) $(SLICE2CPPFLAGS) $<
	mv $(*F)F.h $(HDIR)
	rm -f $(*F)F.cpp

$(HDIR)/%.h %.cpp: $(SDIR)/%.ice $(SLICE2CPP) $(SLICEPARSERLIB)
	rm -f $(HDIR)/$(*F).h $(*F).cpp
	$(SLICE2CPP) $(SLICE2CPPFLAGS) $<
	mv $(*F).h $(HDIR)

%.h %.cpp: %.ice $(SLICE2CPP) $(SLICEPARSERLIB)
	rm -f $(*F).h $(*F).cpp
	$(SLICE2CPP) $(SLICE2CPPFLAGS) $(*F).ice	

#%.h %.cpp: %.y
#	rm -f $(*F).h $(*F).cpp
#	bison $(BISONFLAGS) $<
#	mv $(*F).tab.c $(*F).cpp
#	mv $(*F).tab.h $(*F).h
#	rm -f $(*F).output

#%.cpp: %.l
#	flex $(FLEXFLAGS) $<
#	rm -f $@
#	echo '#include <IceUtil/Config.h>' > $@
#	cat lex.yy.c >> $@
#	rm -f lex.yy.c

all:: $(SRCS) $(TARGETS)

depend:: $(SRCS)  $(patsubst $(SDIR)/%.ice,$(HDIR)/%.h,$(wildcard $(SDIR)/*F.ice)) $(SLICE_SRCS)
	-rm -f .depend
	if test -n "$(SRCS)" ; then \
	    $(CXX) -DMAKEDEPEND -M $(CXXFLAGS) $(CPPFLAGS) $(SRCS) | $(ice_dir)/config/makedepend.py >> .depend; \
	fi
	if test -n "$(SLICE_SRCS)" ; then \
	    $(SLICE2CPP) --depend $(SLICE2CPPFLAGS) $(SLICE_SRCS) | $(ice_dir)/config/makedepend.py >> .depend; \
	fi

clean::
	-rm -f $(TARGETS)
	-rm -f core *.o *.bak

ifneq ($(SLICE_SRCS),)
depend::
	$(foreach slice, $(SLICE_SRCS), echo '$(slice): $$(SLICE2CPP) $$(SLICEPARSERLIB)' >> .depend;)

clean::
	rm -f $(addsuffix .cpp, $(basename $(notdir $(SLICE_SRCS))))
	rm -f $(addsuffix .h, $(basename $(notdir $(SLICE_SRCS))))
endif

ifneq ($(HDIR),)
clean::
	rm -f $(addprefix $(HDIR), $(addsuffix .h, $(basename $(subst $(SDIR),, \
	$(filter $(SDIR)/%.ice, $(SLICE_SRCS))))))
endif

ifneq ($(TEMPLATE_REPOSITORY),)
clean::
	rm -fr $(TEMPLATE_REPOSITORY)
endif

install::
